package com.annotation;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.handler.inter.IWriter;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.util.ExcelHandlerUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;

/**
 * @author xiaoshu
 * @description
 * @date 2023年01月18日 22:13
 */
@Aspect
@Component
public class ExcelResponseAspect {

    @Resource
    HttpServletResponse response;

    @Pointcut("@annotation(com.annotation.ExcelResponse)")
    public void exportExcel(){}

    @Around(value = "exportExcel() && @annotation(excelResponse) ")
    public Object afterReturning(ProceedingJoinPoint proceedingJoinPoint, ExcelResponse excelResponse) throws Throwable {
        Object result = proceedingJoinPoint.proceed();
        if (result instanceof Collection){
            List datas = (List) result;
            if (CollectionUtil.isEmpty(datas)){
                return null;
            }
            executeBigExcel(excelResponse,response, datas);
        }
        return null;
    }

    private static final String XLS ="HSSF";
    private static final String XLSX ="XSSF";

    /**
     * 数据导出
     * @param excel excel
     * @param response response
     * @param datas 数据集合
     */
    private void executeBigExcel(ExcelResponse excel,HttpServletResponse response,List datas) {
        Class dataTarget = excel.dataTarget();
        String downloadName = excel.downloadName();
        // 当配置的 excel 名称为空时，取当前时间
        if (!StringUtils.hasText(downloadName)) {
            downloadName = DateTime.now().toDateStr();
        }
        String headName = excel.headName();
        if (StrUtil.isEmpty(headName)){
            headName=null;
        }
        ExcelType suffix = excel.suffix();
        String sheetName = excel.sheetName();
        ExportParams params = new ExportParams(headName, sheetName, suffix);
        params.setHeight((short) 10);
        IWriter<Workbook> exportBigExcel = ExcelExportUtil.exportBigExcel(params, dataTarget);
        List split = CollectionUtil.split(datas, 10000);
        split.forEach(e->{
            exportBigExcel.write((List)e);
        });
        exportBigExcel.close();
        Workbook workbook = exportBigExcel.get();
        String suffixName = suffix.name();
        if (XLS.equals(suffixName)){
            suffixName=".xls";
        }
        if (XLSX.equals(suffixName)){
            suffixName=".xlsx";
        }
        ExcelHandlerUtil.downLoadExcel(downloadName+suffixName, response,workbook);
    }

}
